package com.xyy.saas.payment.util;

import com.xyy.saas.payment.cores.enums.*;
import com.xyy.saas.payment.dao.model.XydCheckDataPo;
import com.xyy.saas.payment.dao.model.XydRepayPlanDetailPo;
import com.xyy.saas.payment.dao.model.XydRepayPlanPo;
import com.xyy.saas.payment.pojo.vo.XydCheckDataVo;
import com.xyy.saas.payment.pojo.vo.XydRepayPlanDetailVo;
import com.xyy.saas.payment.pojo.vo.XydRepayPlanVo;
import lombok.extern.slf4j.Slf4j;
import org.apache.commons.lang.StringUtils;
import org.springframework.beans.BeanUtils;
import org.springframework.util.CollectionUtils;

import java.lang.reflect.Field;
import java.math.BigDecimal;
import java.util.ArrayList;
import java.util.Collections;
import java.util.List;
import java.util.function.Supplier;

@Slf4j
public class PojoConvertUtil extends BeanUtils {

    /**
     * 空判断
     * @param obj : 实体
     * @param blankFields : 判空字段
     */
    public static void checkBlank(Object obj, List<String> blankFields) throws IllegalAccessException {
        if (null == obj) {
            throw new IllegalAccessException(ResultCodeEnum.PARAM_ERROR.getDisplayMsg());
        }
        Class<?> aClass = obj.getClass();
        Field[] declaredFields = aClass.getDeclaredFields();
        for (Field declaredField : declaredFields) {
            declaredField.setAccessible(true);
            if (blankFields.contains(declaredField.getName())) {
                Object value = declaredField.get(obj);
                if (null == value || StringUtils.isBlank(value.toString())) {
                    log.info("参数:{}不允许为空！", declaredField.getName());
                    throw new IllegalAccessException(ResultCodeEnum.PARAM_ERROR.getDisplayMsg() + "：" + declaredField.getName());
                }
            }
        }
    }

    /**
     * 将具有相同属性的类型进行转换
     */
    public static <T> T convertPojo(Object sources, Supplier<T> target) {
        if (null == sources) {
            return null;
        }
        T t = target.get();
        copyProperties(sources, t);
        return t;
    }


    public static <S, T> List<T> copyList(List<S> sources, Supplier<T> target) {
        if (CollectionUtils.isEmpty(sources)) {
            return Collections.EMPTY_LIST;
        }
        List<T> list = new ArrayList<>(sources.size());
        for (S source : sources) {
            T t = target.get();
            copyProperties(source, t);
            list.add(t);
        }
        return list;
    }

    public static List<XydCheckDataVo> convertXydDataVo(List<XydCheckDataPo> dataPos) {
        if (CollectionUtils.isEmpty(dataPos)) {
            return Collections.EMPTY_LIST;
        }

        List<XydCheckDataVo> dataVos = new ArrayList<>(dataPos.size());
        for (XydCheckDataPo dataPo : dataPos) {
            XydCheckDataVo dataVo = new XydCheckDataVo();
            dataVo.setAccountType(XydAccountTypeEnum.getNameByCode(dataPo.getAccountType()));
            dataVo.setTradeAccount(dataPo.getTradeAccount());
            dataVo.setTradeCode(dataPo.getTradeCode());
            dataVo.setTradeDate(DateUtil.format(dataPo.getTradeDate(), DateUtil.DATEFORMATDAY));
            dataVo.setTradeType(XydTradeTypeEnum.getNameByCode(dataPo.getTradeType()));
            dataVo.setOrderPayNo(dataPo.getOrderPayNo());
            dataVo.setRefundPayNo(dataPo.getRefundPayNo());
            dataVo.setOrderNo(dataPo.getOrderNo());
            dataVo.setRefundNo(dataPo.getRefundNo());
            dataVo.setInOutType(XydInOutTypeEnum.getNameByCode(dataPo.getInOutType()));
            dataVo.setTradeAmt(dataPo.getTradeAmt());
            dataVo.setTradeRivalAccount(dataPo.getTradeRivalAccount());
            dataVo.setTradeRivalName(dataPo.getTradeRivalName());
            dataVo.setOrgId(dataPo.getOrgId());
            dataVo.setOrgName(dataPo.getOrgName());
            dataVo.setMerchantId(dataPo.getMerchantId());
            dataVo.setMerchantName(dataPo.getMerchantName());
            dataVos.add(dataVo);
        }
        return dataVos;
    }
    public static List<XydRepayPlanVo> convertXydRepayPlanVo(List<XydRepayPlanPo> dataPos) {
        if (CollectionUtils.isEmpty(dataPos)) {
            return Collections.EMPTY_LIST;
        }

        List<XydRepayPlanVo> dataVos = new ArrayList<>(dataPos.size());
        for (XydRepayPlanPo dataPo : dataPos) {
            XydRepayPlanVo dataVo = new XydRepayPlanVo();
            dataVo.setOuterOrderNo(dataPo.getOuterOrderNo());
            dataVo.setLoanNo(dataPo.getLoanNo());
            dataVo.setOrderNo(dataPo.getOrderNo());
            dataVo.setPayNo(dataPo.getPayNo());
            dataVo.setLoanAmount(dataPo.getLoanAmount());
            dataVo.setInterest(dataPo.getInterest());
            dataVo.setCreditRate(NumberUtil.decimal2rate(dataPo.getCreditRate()));
            dataVo.setLoanLimit(dataPo.getLoanLimit());
            dataVo.setPayTime(DateUtil.format(dataPo.getPayTime(), DateUtil.DATEFORMATSECOND));
            dataVo.setLoanTime(DateUtil.format(dataPo.getLoanTime(), DateUtil.DATEFORMATDAY));
            dataVo.setDueDate(DateUtil.format(dataPo.getDueDate(), DateUtil.DATEFORMATDAY));
            dataVo.setDueDateActual(DateUtil.format(dataPo.getDueDateActual(), DateUtil.DATEFORMATDAY));
            dataVo.setPaidOff(XydPaidOffEnum.getNameByCode(dataPo.getPaidOff()));
            dataVo.setOrgId(dataPo.getOrgId());
            dataVo.setOrgName(dataPo.getOrgName());
            dataVo.setMerchantId(dataPo.getMerchantId());
            dataVo.setMerchantName(dataPo.getMerchantName());
            dataVo.setTotalPlan(dataPo.getTotalPlan());
            dataVo.setPrincipalPaid(dataPo.getPrincipalPaid());
            dataVo.setPrincipal(dataPo.getPrincipal());
            dataVo.setCurrInterest(dataPo.getCurrInterest());
            dataVo.setInterestPaid(dataPo.getInterestPaid());
            dataVo.setSubsidyInterest(dataPo.getSubsidyInterest());
            dataVo.setSubsidyDays(dataPo.getSubsidyDays());
            dataVo.setLateFee(dataPo.getLateFee());
            dataVo.setDayInDefault(dataPo.getDayInDefault());
            dataVo.setLateFeePaid(dataPo.getLateFeePaid());
            dataVo.setRepayPenalty(dataPo.getRepayPenalty());
            dataVo.setIsOriginalRoute(YesNoEnum.getNameByCode(dataPo.getIsOriginalRoute()));
            dataVos.add(dataVo);
        }
        return dataVos;
    }

    public static List<XydRepayPlanDetailVo> convertXydRepayPlanDetailVo(List<XydRepayPlanDetailPo> dataPos) {
        if (CollectionUtils.isEmpty(dataPos)) {
            return Collections.EMPTY_LIST;
        }

        List<XydRepayPlanDetailVo> dataVos = new ArrayList<>(dataPos.size());
        for (XydRepayPlanDetailPo dataPo : dataPos) {
            XydRepayPlanDetailVo dataVo = new XydRepayPlanDetailVo();
            dataVo.setFlowNo(dataPo.getFlowNo());
            dataVo.setRepayTime(DateUtil.format(dataPo.getRepayTime(), DateUtil.DATEFORMATDAY));
            dataVo.setNotifyType(XydNotifyTypeEnum.getNameByCode(dataPo.getNotifyType()));
            dataVo.setRefundPayNo(dataPo.getRefundPayNo());
            dataVo.setRefundNo(dataPo.getRefundNo());
            dataVo.setRepayLoanAmount(dataPo.getRepayLoanAmount());
            dataVo.setRepayInterest(dataPo.getRepayInterest());
            dataVo.setSubsidyInterest(dataPo.getSubsidyInterest());
            dataVo.setSubsidyDays(dataPo.getSubsidyDays());
            dataVo.setRepayPenalty(dataPo.getRepayPenalty());
            dataVo.setRepaymentType(XydRepaymentTypeEnum.getNameByCode(dataPo.getRepaymentType()));
            dataVo.setStatus(XydStatusEnum.getNameByCode(dataPo.getStatus()));
            dataVos.add(dataVo);
        }
        return dataVos;
    }


}